﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DbMapping.Common;
using VideoCache.Server.BLL;
using VideoCache.Server.Model;
using VideoCache.Server.MVCWeb.Areas.Admin.Models;
using VideoCache.Server.MVCWeb.Attributes;
using VideoCache.Aggregation.Interfaces;
using VideoCache.Aggregation.Implements;

namespace VideoCache.Server.MVCWeb.Areas.Admin.Controllers
{
    [ActionAuthorize(AuthorizeType.Administrator)]
    public class AlbumController : Controller
    {
        public ActionResult Index(string keyword, int typeId = 0, int areaId = 0, int status = 0, bool isEnd = false, int page = 1)
        {
            ViewBag.Keyword = keyword;

            Conditions typeCons = new Conditions();
            typeCons.Add(new ConditionItem(LinkType.And, DictionaryInfo.ParentIdField, CompareType.Equal, 1));
            ViewBag.TypeId = new SelectList(DictionaryBLL.FindAll(typeCons, null), "Id", "Name");

            Conditions areaCons = new Conditions();
            areaCons.Add(new ConditionItem(LinkType.And, DictionaryInfo.ParentIdField, CompareType.Equal, 6));
            ViewBag.AreaId = new SelectList(DictionaryBLL.FindAll(areaCons, null), "Id", "Name");

            Conditions statusCons = new Conditions();
            statusCons.Add(new ConditionItem(LinkType.And, DictionaryInfo.ParentIdField, CompareType.Equal, 14));
            ViewBag.Status = new SelectList(DictionaryBLL.FindAll(statusCons, null), "Id", "Name");

            Orders orders = new Orders();
            orders.Add(new OrderItem(AlbumInfo.ModifyTimeField, OrderDirection.Desc));

            Conditions conditions = new Conditions();

            if (!string.IsNullOrEmpty(keyword)) conditions.Add(new ConditionItem(LinkType.And, AlbumInfo.TitleField, CompareType.StartWith, keyword));
            if (typeId != 0) conditions.Add(new ConditionItem(LinkType.And, AlbumInfo.TypeIdField, CompareType.Equal, typeId));
            if (areaId != 0) conditions.Add(new ConditionItem(LinkType.And, AlbumInfo.AreaIdField, CompareType.Equal, areaId));
            if (status != 0) conditions.Add(new ConditionItem(LinkType.And, AlbumInfo.StatusField, CompareType.Equal, status));

            if (isEnd) conditions.Add(new ConditionItem(LinkType.And, AlbumInfo.IsEndField, CompareType.Equal, isEnd));

            int recordCount = 0;
            int pageSize = 30;

            var list = AlbumBLL.FindPage(conditions, orders, page, pageSize, ref recordCount);

            ViewBag.PageSize = pageSize;
            ViewBag.RecordCount = recordCount;

            return View(list);
        }

        [HttpPost]
        public ActionResult IsAlbumTitleAvaliable(string id, string title)
        {
            int albumId;

            if (!int.TryParse(id, out albumId))
            {
                albumId = 0;
            }

            Conditions cons = new Conditions();

            cons.Add(new ConditionItem(LinkType.And, AlbumInfo.IdField, CompareType.NotEqual, albumId));
            cons.Add(new ConditionItem(LinkType.And, AlbumInfo.TitleField, CompareType.Equal, title));

            return Json(!AlbumBLL.Exists(cons));
        }

        public ActionResult Create()
        {
            List<DictionaryInfo> list = DictionaryBLL.FindAll(new Conditions(), new Orders());

            ViewBag.TypeId = new SelectList(list.FindAll(p => p.ParentId == 1), "Id", "Name");
            ViewBag.AreaId = new SelectList(list.FindAll(p => p.ParentId == 6), "Id", "Name");
            ViewBag.Status = new SelectList(list.FindAll(p => p.ParentId == 14), "Id", "Name", 16);

            return View(new AlbumModel());
        }

        [HttpPost]
        public ActionResult Create(AlbumModel model)
        {
            if (ModelState.IsValid)
            {
                model.Save();

                return RedirectToAction("Index");
            }

            return View(model);
        }

        public ActionResult Search(string q)
        {
            IAlbumSearcher searcher = new BaiduAlbumSearcher();
            var result = searcher.Search(q);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Edit(int id)
        {
            try
            {
                AlbumModel model = new AlbumModel(id);

                List<DictionaryInfo> list = DictionaryBLL.FindAll(new Conditions(), new Orders());

                ViewBag.TypeId = new SelectList(list.FindAll(p => p.ParentId == 1), "Id", "Name", model.TypeId);
                ViewBag.AreaId = new SelectList(list.FindAll(p => p.ParentId == 6), "Id", "Name", model.AreaId);
                ViewBag.Status = new SelectList(list.FindAll(p => p.ParentId == 14), "Id", "Name", model.Status);

                return View(model);
            }
            catch (Exception ex)
            {
                return HttpNotFound(ex.Message);
            }
        }

        [HttpPost]
        public ActionResult Edit(AlbumModel model)
        {
            if (ModelState.IsValid)
            {
                model.Save();

                return RedirectToAction("Index");
            }

            return View(model);
        }

        public ActionResult Delete(int id)
        {
            AlbumBLL.DeleteAlbum(id);

            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Delete(string ids)
        {
            try
            {
                List<int> list = new List<int>();

                string[] intList = ids.Split(',');

                foreach (string strId in intList)
                {
                    int id;

                    if (int.TryParse(strId, out id))
                    {
                        list.Add(id);
                    }
                }

                AlbumBLL.Delete(list.ToArray());

                if (Request.IsAjaxRequest())
                {
                    return Json(null);
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            catch (Exception ex)
            {
                return Json(ex.Message);
            }
        }
    }
}